//获取应用实例
const app = getApp()

Page({
  data: {
    noticeContent: '',
    title: '',
    createTimeStr: '',
    noticeTypeStr: '',
    isLike: 0,
    id: null,
    wh:0
  },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  onShow: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "wh": res.windowHeight - 48 -wx.getSystemInfoSync().statusBarHeight
        })
      }
    })
  },
  onLoad: function (options) {
    var id = options.id;
    this.setData({
      id: id
    });
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/notice/detail',
      data: {
        id: id,
        userId: app.globalData.userId
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          that.data.noticeContent = res.data.record.noticeContent;
          that.data.title = res.data.record.title;
          that.data.createTimeStr = res.data.record.createTimeStr;
          that.data.isLike = res.data.record.isLike;
          var noticeTypeStr = '';
          if (res.data.record.noticeType == '1'){
            noticeTypeStr = '通知公告';
          } else if (res.data.record.noticeType == '2') {
            noticeTypeStr = '党建要闻';
          } else if (res.data.record.noticeType == '3') {
            noticeTypeStr = '标准化建设';
          } else {
            noticeTypeStr = '组织动态';
          }
          wx.setNavigationBarTitle({
            title: noticeTypeStr
          })
          that.data.noticeTypeStr = noticeTypeStr;
          let richText = that.data.noticeContent;  
      // 正则表达式真的很重要，所以我要学习一下
      richText = richText.replace(/\<p>/gi, '<p class="p_class">').replace(/\<h1>/gi, '<h1 class="p_class">')
      console.log(richText)
          that.setData({
            title: that.data.title,
            noticeContent: richText,
            createTimeStr: that.data.createTimeStr,
            noticeTypeStr: that.data.noticeTypeStr,
            isLike: that.data.isLike
          })
        }
      }
    })
  },
  like: function (e) {
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/like/save',
      data: {
        userId: app.globalData.userId,
        contentId: this.data.id,
        contentType: 1
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          that.setData({
            isLike: 1
          });
        }
        else {
        }
      }
    })
  },
  unlike: function (e) {
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/like/deleteByRecord',
      data: {
        userId: app.globalData.userId,
        contentId: this.data.id,
        contentType: 1
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          that.setData({
            isLike: 0
          });
        }
        else {
        }
      }
    })
  }
})
